﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace com.tiyma.components
{
    public class FileUploadManager
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        public void UpLoadFile()
        {
            try
            {
                SysConfig t_config = SerializationHelper.Load(typeof(SysConfig), "") as SysConfig;

                if (t_config == null)
                    return;

                DirectoryInfo t_dir = new DirectoryInfo(t_config.FilePath);

                //创建备份目录
                string t_bak_path = System.IO.Path.Combine(t_dir.Parent.FullName, "book_chapter_bak");
                t_bak_path = System.IO.Path.Combine(t_dir.Parent.FullName, string.Format("data_{0}_bak", DateTime.Now.ToString("yyyyMM")));
                if (System.IO.Directory.Exists(t_bak_path) == false)
                {
                    System.IO.Directory.CreateDirectory(t_bak_path);
                }

                FTPManager t_ftp_mgr = new FTPManager(new Uri(string.Format("ftp://{0}/", t_config.FTPServer)), t_config.FTPUserName, AES.DecryptString(t_config.FTPPassword));

                bool t_ret = false;

                List<System.IO.FileInfo> t_list = new List<System.IO.FileInfo>();

                foreach (System.IO.FileInfo t_file in t_dir.GetFiles("*-sql.txt"))
                {
                    t_list.Add(t_file);
                }

                //按创建时间排序
                if (t_list != null && t_list.Count > 0)
                {
                    t_list.Sort(delegate(System.IO.FileInfo t_a, System.IO.FileInfo t_b)
                    {
                        if (t_a.CreationTime < t_b.CreationTime)
                            return -1;
                        else if (t_a.CreationTime == t_b.CreationTime)
                            return 0;
                        return 1;
                    });
                }

                for (int index = 0; index < t_list.Count - 1; index++)
                {
                    t_ret = t_ftp_mgr.UploadFile(t_list[index].FullName, true);

                    if (t_ret)
                    {
                        if (!File.Exists(Path.Combine(t_bak_path, t_list[index].Name)))
                        {
                            File.Move(t_list[index].FullName, Path.Combine(t_bak_path, t_list[index].Name));
                        }
                    }
                }

                t_list = new List<FileInfo>();

                foreach (System.IO.FileInfo t_file in t_dir.GetFiles("*-data.txt"))
                {
                    t_list.Add(t_file);
                }

                //按创建时间排序
                if (t_list != null && t_list.Count > 0)
                {
                    t_list.Sort(delegate(System.IO.FileInfo t_a, System.IO.FileInfo t_b)
                    {
                        if (t_a.CreationTime < t_b.CreationTime)
                            return -1;
                        else if (t_a.CreationTime == t_b.CreationTime)
                            return 0;
                        return 1;
                    });
                }

                for (int index = 0; index < t_list.Count - 1; index++)
                {
                    t_ret = t_ftp_mgr.UploadFile(t_list[index].FullName, true);

                    if (t_ret)
                    {
                        if (!File.Exists(Path.Combine(t_bak_path, t_list[index].Name)))
                        {
                            File.Move(t_list[index].FullName, Path.Combine(t_bak_path, t_list[index].Name));
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error("上传文件失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 服务上传文件
        /// </summary>
        public void UploadFileByService()
        {
            while (true)
            {
                System.Threading.Thread.Sleep(100);

                UpLoadFile();

                System.Threading.Thread.Sleep(AppConfig.UpLoadInterval * 1000 * 60);
            }
        }
    }
}
